package com.example.friendcash.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import com.astuetz.PagerSlidingTabStrip;
import com.example.friendcash.BaseApplication;
import com.example.friendcash.R;
import com.example.friendcash.database.core.Event;
import com.example.friendcash.database.services.interfaces.IEventService;
import com.example.friendcash.database.services.interfaces.IServiceFactory;
import com.example.friendcash.fragments.ExpensesFragment;
import com.example.friendcash.fragments.FriendsFragment;
import com.example.friendcash.fragments.PaymentFragment;

import static com.example.friendcash.activities.MainActivity.EVENT_POSITION;

public class EventActivity extends AbsMenuActivity {

    private IServiceFactory serviceFactory;

    private int eventPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.event);
        setupOverflowMenu(R.menu.event, findViewById(R.id.event_menu));
        final Bundle extras = getIntent().getExtras();
        if (extras != null) {
            eventPosition = extras.getInt(EVENT_POSITION);
        }
        serviceFactory = BaseApplication.getInstance().getServiceFactory();
        final IEventService eventService = serviceFactory.createEventService();
        final Event event = eventService.getAllEvents().get(eventPosition);
        final TextView eventName = (TextView) findViewById(R.id.event_name);
        eventName.setText(event.name);
        setupViewPager();
    }

    private void setupViewPager() {
        // setup view pager
        final SectionsPagerAdapter adapter = new SectionsPagerAdapter(
                getSupportFragmentManager());
        final ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
        pager.setCurrentItem(1);
        // setup tabs
        final PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setTextColorResource(R.color.fc_purple);
        tabs.setViewPager(pager);
        tabs.setTextSize(getResources().getDimensionPixelSize(R.dimen.text_size_small));
        tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                adapter.getItem(arg0).setUserVisibleHint(true);
                // hide keyboard when fragment changed
                if (getCurrentFocus() != null) {
                    final InputMethodManager imm = (InputMethodManager)
                            getSystemService(INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) { }
            @Override
            public void onPageScrollStateChanged(int arg0) { }
        });
    }

    public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0: return new ExpensesFragment();
                case 1: return new FriendsFragment();
                case 2: return new PaymentFragment();
                default: return new ExpensesFragment();
            }
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0: return "Expenses";
                case 1: return "Friends";
                case 2: return "Payback";
                default: return null;
            }
        }
    }

}
